package com.dwx.guli.service.edu.controller.admin;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dwx.guli.service.base.result.R;
import com.dwx.guli.service.edu.entity.query.CourseQuery;
import com.dwx.guli.service.edu.entity.query.form.CourseInfoForm;
import com.dwx.guli.service.edu.entity.vo.CoursePublishVo;
import com.dwx.guli.service.edu.entity.vo.CourseVo;
import com.dwx.guli.service.edu.feign.OssFileService;
import com.dwx.guli.service.edu.service.CourseService;
import com.dwx.guli.service.edu.service.VideoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author dwx
 * @since 2021-06-30
 */
@Api(tags = "课程列表")
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/admin/edu/course")
public class CourseController {
    @Autowired
    private CourseService courseService;
    @Autowired
    private VideoService videoService;
    @ApiOperation("添加课程")
    @PostMapping("/save")
    public R save(
            @ApiParam(value = "课程表单",required = true)
            @RequestBody CourseInfoForm courseInfoForm){
        String courseId=courseService.saveCourseInfo(courseInfoForm);
        return R.ok().data("courseId",courseId);
    }
    @ApiOperation("分页课程列表")
    @GetMapping("/list/{page}/{limit}")
    public R pageList(
            @ApiParam(value = "当前页码", required = true)
            @PathVariable("page") Long page,
            @ApiParam(value = "条目数", required = true)
            @PathVariable("limit") Long limit,
            @ApiParam(value = "查询条件", required = false)
            CourseQuery courseQuery){
        IPage<CourseVo> courseVoIPage = courseService.selectPage(page, limit, courseQuery);
        List<CourseVo> records = courseVoIPage.getRecords();
        long total = courseVoIPage.getTotal();
        return R.ok().data("items",records).data("total",total);
    }
    @ApiOperation("根据ID查询课程")
    @GetMapping("/course-info/{id}")
    public R getById(
            @ApiParam(value = "课程ID", required = true)
            @PathVariable String id) {
        CourseInfoForm courseInfoForm=courseService.getCourseInfoById(id);
        if(courseInfoForm!=null){
            return R.ok().data("item",courseInfoForm);
        }else {
            return R.error().message("数据不存在!");
        }
    }
    @ApiOperation("更新课程")
    @PutMapping("update-course-info")
    public R updateCourseInfoById(
            @ApiParam(value = "课程基本信息", required = true)
            @RequestBody CourseInfoForm courseInfoForm){
        courseService.updateCourseInfoById(courseInfoForm);
        return R.ok().message("修改成功");
    }
    @ApiOperation("根据ID删除课程")
    @DeleteMapping("remove/{id}")
    public R removeById(
            @ApiParam(value = "课程ID", required = true)
            @PathVariable String id){
        //删除云端视频
        //TODO
        videoService.removeMediaVideoByCourseId(id);
        //删除封面图片
        boolean b=courseService.removeCoverById(id);
        if(!b){
            log.warn("讲师课程封面失败，课程id： " + id);
        }
        //删除课程
        boolean result=courseService.removeCourseById(id);
        if(result){
            return R.ok().message("删除课程成功");
        }else {
            return R.error().message("删除课程失败");
        }
    }
    @ApiOperation("根据ID获取课程发布信息")
    @GetMapping("course-publish/{id}")
    public R getCoursePublishVoById(
            @ApiParam(value = "课程ID", required = true)
            @PathVariable String id){
        CoursePublishVo coursePublishVo=courseService.getCoursePublishVoById(id);
        if(coursePublishVo!=null){
            return R.ok().data("item",coursePublishVo);
        }else {
            return R.error().message("数据不存在");
        }
    }
    @ApiOperation("根据id发布课程")
    @PutMapping("publish-course/{id}")
    public R publishCourseById(
            @ApiParam(value = "课程ID", required = true)
            @PathVariable String id){
        boolean result = courseService.publishCourseById(id);
        if (result) {
            return R.ok().message("发布成功");
        } else {
            return R.error().message("数据不存在");
        }
    }
}

